package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.astrob.model.LonLat;
import com.astrob.nhcmap.R;
import com.astrob.view.BaseViewHolder;
import com.nhc.model.DJZParamInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.base.BaseAdapter;
import zuo.biao.library.model.Entry;
import zuo.biao.library.ui.PageScroller;
import zuo.biao.library.ui.TopMenuWindow;

public class DJZParamActivity extends BaseActivity {
    private final String TAG = "DJZParamActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, DJZParamActivity.class);
    }
    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.djzparam_activity);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>
    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private ListView lv_param;

    //示例代码>>>>>>>>
    @Override
    public void initView() {//必须在onCreate方法内调用
        autoSetTitle();//自动设置标题为上个Activity传入的INTENT_TITLE

        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        lv_param = (ListView) findViewById(R.id.lv_param);
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    //示例代码<<<<<<<<
    private DemoAdapter adapter;
    //示例代码>>>>>>>>

    /**
     * 示例方法 ：显示列表内容
     *
     * @param list
     */
    private void setList(List<Entry<Integer, String>> list) {
        if (adapter == null) {
            adapter = new DemoAdapter(context);
            lv_param.setAdapter(adapter);
        }
        adapter.refresh(list);
    }
    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private List<Entry<Integer, String>> locationlist;
    private ArrayList<DJZParamInfo> mDpList = new ArrayList<DJZParamInfo>();

    private void resetDpInfo() {

        for (int i = 0; i < 4; i++) {
            mDpList.add(new DJZParamInfo(i * 2 + "", (i + 1) + "", new LonLat(112.0124, 21.0535), new LonLat(112.0124, 21.0535)));
        }
    }

    //示例代码>>>>>>>>>
    @Override
    public void initData() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        showProgressDialog(R.string.loading);
        runThread(TAG + "initData", new Runnable() {
            @Override
            public void run() {
                resetDpInfo();
                locationlist = getList();
                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                        setList(locationlist);
                    }
                });
            }
        });
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    protected List<Entry<Integer, String>> getList() {
        locationlist = new ArrayList<Entry<Integer, String>>();
        for (int i = 0; i < mDpList.size(); i++) {
            locationlist.add(new Entry<Integer, String>(i, ""));
        }
        return locationlist;
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<
        lv_param.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

        //分页滚动示例代码<<<<<<<<<<<<<<<<<<<
        new PageScroller(lv_param).init();
        //分页滚动示例代码>>>>>>>>>>>>>>>>>>>

        //示例代码>>>>>>>>>>>>>>>>>>>
    }

    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //@user 新增方法

    @Override
    public void onForwardClick(View v) {
        //top栏右边按钮响应
        toActivity(TopMenuWindow.createIntent(context, getResources().getStringArray(R.array.top_menu_djzparam)), REQUEST_TO_TOP_MENU, false);
    }

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    private final int REQUEST_TO_TOP_MENU = 30;
    private final int REQUEST_TO_TOP_ADD = 31;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }

        switch (requestCode) {
            case REQUEST_TO_TOP_MENU:
                if (data != null) {
                    switch (data.getIntExtra(TopMenuWindow.RESULT_POSITION, -1)) {
                        case 0:
                            toActivity(DJZAddPtActivity.createIntent(this), REQUEST_TO_TOP_ADD);
                            break;
                        case 1:
                            break;
                        case 2:
                            break;
                        case 3:
                            break;
                        default:
                            break;
                    }
                }
                break;
        }
    }

    //Adapter
    public class DemoAdapter extends BaseAdapter<Entry<Integer, String>> {
        public DemoAdapter(Activity context) {
            super(context);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.djzparam_view, parent, false);
            }

            TextView tv_title_0 = BaseViewHolder.get(convertView, R.id.tv_title_0);
            TextView tv_title_1 = BaseViewHolder.get(convertView, R.id.tv_title_1);
            TextView tv_name_0 = BaseViewHolder.get(convertView, R.id.tv_name_0);
            TextView tv_name_1 = BaseViewHolder.get(convertView, R.id.tv_name_1);
            TextView tv_lat = BaseViewHolder.get(convertView, R.id.tv_lat);
            TextView tv_lon = BaseViewHolder.get(convertView, R.id.tv_lon);
            TextView tv_E = BaseViewHolder.get(convertView, R.id.tv_E);
            TextView tv_N = BaseViewHolder.get(convertView, R.id.tv_N);

            final Entry<Integer, String> kvb = getItem(position);
            int index = kvb.getKey();
            if (index >= 0 && index < mDpList.size()) {
                DJZParamInfo dp = mDpList.get(index);
                tv_title_0.setText(getString(R.string.kzd_t) + (index + 1));
                tv_title_1.setText(getString(R.string.gps_t) + (index + 1));
                tv_lat.setText(getString(R.string.lat_tt) + (float)dp.getLat());
                tv_lon.setText(getString(R.string.lon_tt) + (float)dp.getLon());
                tv_E.setText(getString(R.string.ezb_t) + (float)dp.getE());
                tv_N.setText(getString(R.string.nzb_t) + (float)dp.getN());
            }

            return super.getView(position, convertView, parent);
        }

        private final long serialVersionUID = 3L;

        @Override
        public long getItemId(int position) {
//            return getItem(position).getId();
            return serialVersionUID + position;
        }
    }

}
